﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DragonBones;

public class AnimationControl : MonoBehaviour {

	private const string Head_Group = "head";
	private const string Body_Group = "body";

	private UnityArmatureComponent body = null;
	private UnityArmatureComponent head = null;

	private string headState = "idle";
	private string bodyState = "idle";

	void Start () {
		if (this.transform.FindChild ("head") == null) {
			head = null;
		} else {
			head = this.transform.FindChild ("head").GetComponent<UnityArmatureComponent>();
			head.animation.Reset ();
			head.animation.FadeIn (headState, -1, -1, 0, Head_Group);
		}

		body = this.transform.FindChild("body").GetComponent<UnityArmatureComponent> ();
		body.animation.Reset ();
		body.animation.FadeIn (bodyState, -1, -1, 0, Body_Group);


	}

	int Check(string[] currentState){
		if (head != null) {
			if (currentState [0] == headState && currentState [1] == bodyState) {
				return 0;
			} else if (currentState [0] == headState && currentState [1] != bodyState) {
				bodyState = currentState [1];
				return 1;
			} else if (currentState [0] != headState && currentState [1] == bodyState) {
				headState = currentState [0];
				return 2;
			} else {
				headState = currentState [0];
				bodyState = currentState [0];
				return 3;
			}

//			}
		}
		return 200;
	}

	public void UpdateAnimation(params string[] state){
		if (head != null) {
			switch (Check (state)) {
			case 0:
				break;
			case 1:
				body.animation.FadeIn (state [1], -1f, -1, 0, Body_Group);
				break;
			case 2:
				head.animation.FadeIn (state [0], -1f, -1, 0, Head_Group);
				break;
			case 3:
				body.animation.FadeIn (state [1], -1f, -1, 0, Body_Group);
				head.animation.FadeIn (state [0], -1f, -1, 0, Head_Group);
				break;
			default:
				break;
			}
		} else {
			
		}
	}
}
